﻿using LoA.Shared.Input;
using LoA.Shared.Interfaces;
using LoA.Shared.Physics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace LoA.CharacterEngine {
	public partial class CharacterComponent : DrawableGameComponent, ISuspendableGameComponent {
		MouseState? lastMouseState = null;
		MouseState originalMouseState;

		public override void Update(GameTime gameTime) {
			if (!this.Suspended && this.Visible) {
				IInputManager inputManager = Input.GetInputManager(PlayerIndex.One);

				if (inputManager.DirectionButtons != InputDirection.None) {
					if (inputManager.Matches(InputDirection.TurnLeft))
						this.Physics.Turn(this.character, Rotation.TurnLeft, MathHelper.Pi * (float)gameTime.ElapsedGameTime.TotalSeconds);
					if (inputManager.Matches(InputDirection.TurnRight))
						this.Physics.Turn(this.character, Rotation.TurnRight, MathHelper.Pi * (float)gameTime.ElapsedGameTime.TotalSeconds);

					if (inputManager.Matches(InputDirection.Up))
						this.Physics.Move(this.character, Direction.Forward, 7f * (float)gameTime.ElapsedGameTime.TotalSeconds);
					if (inputManager.Matches(InputDirection.Down))
						this.Physics.Move(this.character, Direction.Backward, 3.5f * (float)gameTime.ElapsedGameTime.TotalSeconds);

					if (inputManager.Matches(InputDirection.Left))
						this.Physics.Move(this.character, Direction.Left, 2.5f * (float)gameTime.ElapsedGameTime.TotalSeconds);
					if (inputManager.Matches(InputDirection.Right))
						this.Physics.Move(this.character, Direction.Right, 2.5f * (float)gameTime.ElapsedGameTime.TotalSeconds);

					Map.UpdatePosition(this.Position);
					Camera.UpdateTarget(this.Position, this.Orientation);
				}

				MouseState mouseState = Mouse.GetState( );
				if (mouseState.MiddleButton == ButtonState.Pressed) {
					if (lastMouseState == null) {
						originalMouseState = mouseState;
						Mouse.SetPosition(this.GraphicsDevice.Viewport.Width / 2, this.GraphicsDevice.Viewport.Height / 2);
						MouseState mouseState2 = Mouse.GetState( );
						lastMouseState = mouseState2;
						this.Game.IsMouseVisible = false;
					} else {
						int x = lastMouseState.Value.X - mouseState.X;
						int y = lastMouseState.Value.Y - mouseState.Y;

						float xP = x * 1.0f / this.GraphicsDevice.Viewport.Width;
						float yP = y * 1.0f / this.GraphicsDevice.Viewport.Height;

						this.Physics.Turn(this.character, Rotation.TurnRight, MathHelper.Pi * xP);

						Camera.UpdateTarget(this.Position, this.Orientation);

						Mouse.SetPosition(this.GraphicsDevice.Viewport.Width / 2, this.GraphicsDevice.Viewport.Height / 2);
						MouseState mouseState2 = Mouse.GetState( );
						lastMouseState = mouseState2;
					}
				} else {
					if (lastMouseState != null) {
						Mouse.SetPosition(originalMouseState.X, originalMouseState.Y);
						lastMouseState = null;
						this.Game.IsMouseVisible = true;
					}
				}
			}

			base.Update(gameTime);
		}
	}
}
